using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Resources;
using PickGold.Charting;

namespace PickGold.Charting.ChartTypes
{
	internal class ChartTypeRegistry : IServiceProvider, IDisposable
	{
		private Hashtable _createdChartTypes = new Hashtable(StringComparer.OrdinalIgnoreCase);
		private System.Resources.ResourceManager _resourceManager;
		internal Hashtable registeredChartTypes = new Hashtable(StringComparer.OrdinalIgnoreCase);

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				foreach (string str in this._createdChartTypes.Keys)
				{
					((IChartType)this._createdChartTypes[str]).Dispose();
				}
				this._createdChartTypes.Clear();
			}
		}

		public IChartType GetChartType(string name)
		{
			if (!this.registeredChartTypes.Contains(name))
			{
				throw new ArgumentException(SR.ExceptionChartTypeUnknown(name));
			}
			if (!this._createdChartTypes.Contains(name))
			{
				this._createdChartTypes[name] = ((Type)this.registeredChartTypes[name]).Assembly.CreateInstance(((Type)this.registeredChartTypes[name]).ToString());
			}
			return (IChartType)this._createdChartTypes[name];
		}

		public IChartType GetChartType(SeriesChartType chartType)
		{
			return this.GetChartType(Series.GetChartTypeName(chartType));
		}

		public void Register(string name, Type chartType)
		{
			if (this.registeredChartTypes.Contains(name))
			{
				if (this.registeredChartTypes[name].GetType() != chartType)
				{
					throw new ArgumentException(SR.ExceptionChartTypeNameIsNotUnique(name));
				}
			}
			else
			{
				bool flag = false;
				foreach (Type type in chartType.GetInterfaces())
				{
					if (type == typeof(IChartType))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					throw new ArgumentException(SR.ExceptionChartTypeHasNoInterface);
				}
				this.registeredChartTypes[name] = chartType;
			}
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		object IServiceProvider.GetService(Type serviceType)
		{
			if (serviceType != typeof(ChartTypeRegistry))
			{
				throw new ArgumentException(SR.ExceptionChartTypeRegistryUnsupportedType(serviceType.ToString()));
			}
			return this;
		}

		public System.Resources.ResourceManager ResourceManager
		{
			get
			{
				if (this._resourceManager == null)
				{
					this._resourceManager = new System.Resources.ResourceManager(typeof(Chart).Namespace + ".Design", Assembly.GetExecutingAssembly());
				}
				return this._resourceManager;
			}
		}
	}
}

